﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Timthoy.Ding
 *      Date:           2013/6/24 21:48:13
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Medical.EntityType;
using Sector7.Framework.DB;
using Sector7.Medical.Consts;

namespace Sector7.Medical.DAO.Standard
{
    /// <summary>
    /// <!-- The description about this class. -->
    /// </summary>
    public class SickRoomDAO : DAOBase<M7_SickRoom>, ISickRoomDAO
    {
        #region Public functions

        /// <summary>
        /// 获取所有病区信息
        /// </summary>
        /// <returns></returns>
        public List<M7_SickRoom> ReadAll()
        {
            string sql = @"
                SELECT 
                    s.RoomId,
                    s.RoomName,
                    s.SpellCode,
                    s.DeptId,
                    s.IsValid,
                    s.Remarks,
                    de.DeptId,
                    de.DeptName
                    FROM M7_SickRoom s,M7_Department de
                    WHERE 1 = 1
                    AND s.DeptId = de.DeptId";

            using (DBHelper helper = new DBHelper())
            {
                List<M7_SickRoom> roomList = new List<M7_SickRoom>();

                SqlDataReader reader = helper.ExecuteQuery(sql);
                while (reader.Read())
                {
                    var room = new M7_SickRoom();
                    DBUtil.ReadData(reader, room);

                    var dept = new M7_Department();
                    DBUtil.ReadData(reader, dept);
                    room.Department = dept;

                    roomList.Add(room);
                }

                return roomList;
            }
        }

        /// <summary>
        /// 添加科室
        /// </summary>
        /// <param name="area">病区信息</param>
        /// <returns></returns>
        public override object Create(M7_SickRoom room)
        {
            string sql = @"
                INSERT INTO M7_SickRoom
                        (RoomName
                        ,SpellCode
                        ,DeptId
                        ,IsValid
                        ,Remarks)
                    VALUES
                        (@RoomName
                        ,@SpellCode
                        ,@DeptId
                        ,@IsValid
                        ,@Remarks)
                SELECT @@IDENTITY";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteScalar(sql,
                    DBUtil.CreateSqlParameter(room.RoomName, "RoomName"),
                    DBUtil.CreateSqlParameter(room.SpellCode, "SpellCode"),
                    DBUtil.CreateSqlParameter(room.DeptId, "DeptId"),
                    DBUtil.CreateSqlParameter(room.IsValid, "IsValid"),
                    DBUtil.CreateSqlParameter(room.Remarks, "Remarks"));
            }
        }

        #endregion

        #region Structure
        public SickRoomDAO()
        {
            m_tableName = C_M7_TableName.SICK_ROOM;
            m_primaryKeyName = C_ColumnName.ROOM_ID;
        }
        #endregion
    }
}
